/**
 * This file is part of Lineage II Client Data Parser.
 * 
 * Lineage II Client Data Parser is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * Lineage II Client Data Parser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Lineage II Client Data Parser.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.l2emuproject.entity;

import net.l2emuproject.entity.base.DefaultEntity;

/**
 * @author Savormix
 *
 */
public final class Recipe extends DefaultEntity
{
	private static final String[]	CRAFT		=
												{ "dwarven", "common" };

	private final String			name;
	private final int				id;
	private final int				rpID;
	private final int				level;
	private final int				productID;
	private final int				productCount;
	private final int				mp;
	private final int				success;
	private final int[]				matID;
	private final int[]				matCount;
	private final int				matExtra;
	// Deterministic
	private boolean					common		= false;
	// Experimental
	private int						masterwork	= 0;

	/**
	 * Creates a recipe instance.
	 * @param name Recipe name
	 * @param id Recipe ID
	 * @param rpID Recipe item ID
	 * @param level Recipe level
	 * @param productID Product item ID
	 * @param productCount Product count
	 * @param mp Consumed Mana Points
	 * @param success Success rate %
	 * @param matID Material item ID array
	 * @param matCount Material count array
	 * @param matExtra unknown
	 */
	public Recipe(String name, int id, int rpID, int level, int productID, int productCount, int mp, int success, int[] matID, int[] matCount, int matExtra)
	{
		this.name = name;
		this.id = id;
		this.rpID = rpID;
		this.level = level;
		this.productID = productID;
		this.productCount = productCount;
		this.mp = mp;
		this.success = success;
		this.matID = matID;
		this.matCount = matCount;
		this.matExtra = matExtra;
	}

	/**
	 * @return Craft type: Common/Dwarven
	 */
	public final boolean isCommon()
	{
		return common;
	}

	/**
	 * @param common the craft type to set
	 */
	public final void setCommon(boolean common)
	{
		this.common = common;
	}

	/**
	 * @return the name
	 */
	public final String getName()
	{
		return name;
	}

	/**
	 * @return the ID
	 */
	public final int getId()
	{
		return id;
	}

	/**
	 * @return the recipe item ID
	 */
	public final int getRpID()
	{
		return rpID;
	}

	/**
	 * @return the level
	 */
	public final int getLevel()
	{
		return level;
	}

	/**
	 * @return the product item ID
	 */
	public final int getProductID()
	{
		return productID;
	}

	/**
	 * @return the product count
	 */
	public final int getProductCount()
	{
		return productCount;
	}

	/**
	 * @return the MP consumed
	 */
	public final int getMp()
	{
		return mp;
	}

	/**
	 * @return the success rate
	 */
	public final int getSuccess()
	{
		return success;
	}

	/**
	 * @return the material item IDs
	 */
	public final int[] getMatID()
	{
		return matID;
	}

	/**
	 * @return the material counts
	 */
	public final int[] getMatCount()
	{
		return matCount;
	}

	/**
	 * @return 1 if can produce masterwork item, 0 if not
	 */
	public final int getMatExtra()
	{
		return matExtra;
	}

	/**
	 * @return masterwork product ID
	 */
	public final int getMasterwork()
	{
		return masterwork;
	}

	/**
	 * @return craft type as String
	 */
	public final String getCraftType()
	{
		return CRAFT[common ? 1 : 0];
	}

	private final boolean containsMat(int itemID)
	{
		for (int material : matID)
			if (material == itemID)
				return true;
		return false;
	}

	/* (non-Javadoc)
	 * @see com.l2cdp.entity.base.IAnalysis#deterministicAnalysis()
	 */
	@Override
	public void deterministicAnalysis()
	{
		// Only common recipes need fish stuff
		for (int i = 6908; i <= 6916; i++)
			if (containsMat(i))
				common = true;
		// Mysterious Solvent - Elixirs
		if (containsMat(8658))
			common = true;
		// Magic sign - Philosopher stone stuff/Magic reagent
		for (int i = 9168; i <= 9170; i++)
			if (containsMat(i))
				common = true;
	}

	// TODO: once item parser is done, look up masterwork items
	/* (non-Javadoc)
	 * @see com.l2cdp.entity.base.IAnalysis#experimentalAnalysis()
	 */
	@Override
	public void experimentalAnalysis()
	{ /* Your ideas here */
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return getName() + "," + getCraftType() + " Lv" + getLevel();
	}
}
